package com.ng.atuan.demo;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.LocationSource;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.ng.atuan.R;

/**
 * Created by niangang on 2016/12/22.
 */

public class MyActivity extends CheckPermissionsActivity implements
        AMap.OnMarkerClickListener, AMap.OnInfoWindowClickListener, AMap.OnMarkerDragListener,
        AMap.OnMapLoadedListener, AMap.InfoWindowAdapter, AMap.OnCameraChangeListener, LocationSource, AMapLocationListener {
    private static final LatLng marker1 = new LatLng(34.19738, 108.88009);
    private static final LatLng marker2 = new LatLng(34.25426, 108.88332);
    private static final LatLng marker3 = new LatLng(34.36426, 108.88342);
    private static final LatLng marker4 = new LatLng(34.27426, 108.88352);
    private static final LatLng marker5 = new LatLng(34.58426, 108.88362);
    private AMap mAMap;
    private MapView mMapView;

    OnLocationChangedListener mListener;
    AMapLocationClient mlocationClient;
    AMapLocationClientOption mLocationOption;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.marker);
        mMapView = (MapView) findViewById(R.id.map);
        mMapView.onCreate(savedInstanceState);
        init();
    }

    private void init() {

        if (mAMap == null) {
            mAMap = mMapView.getMap();
            mAMap.getUiSettings().setRotateGesturesEnabled(false);
            mAMap.moveCamera(CameraUpdateFactory.zoomBy(5));
            setUpMap();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        mMapView.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume ()，实现地图生命周期管理
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause ()，实现地图生命周期管理
        mMapView.onPause();
    }

    private void setUpMap() {

        mAMap.getUiSettings().setZoomControlsEnabled(true); // 显示默认的定位按钮
        mAMap.getUiSettings().setMyLocationButtonEnabled(true);// 隐藏缩放按钮
        mAMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);
        mAMap.setOnMarkerClickListener(this);// 设置点击marker事件监听器
        mAMap.setOnInfoWindowClickListener(this);// 设置点击infoWindow事件监听器
        mAMap.setInfoWindowAdapter(this);// 设置自定义InfoWindow样式
        mAMap.setOnMarkerDragListener(this);// 设置marker可拖拽事件监听器
        mAMap.setOnMapLoadedListener(this);// 设置amap加载成功事件监听器
        mAMap.setLocationSource(this);
        mAMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位，false表示隐藏定位层并不可触发定位，默认是false

        addMarkersToMap();// 往地图上添加marker

    }

    private void addMarkersToMap() {
        mAMap.addMarker(new MarkerOptions().position(marker1).title("title")
                .snippet("snippet")
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.green)));
        mAMap.addMarker(new MarkerOptions().position(marker2).title("title")
                .snippet("snippet")
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.green)));
        mAMap.addMarker(new MarkerOptions().position(marker3).title("title")
                .snippet("snippet")
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.green)));
        mAMap.addMarker(new MarkerOptions().position(marker4).title("title")
                .snippet("snippet")
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.green)));
        mAMap.addMarker(new MarkerOptions().position(marker5).title("title")
                .snippet("snippet")
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.green)));
    }

    @Override
    public View getInfoWindow(Marker marker) {
        return null;
    }

    @Override
    public View getInfoContents(Marker marker) {
        return null;
    }

    @Override
    public void onCameraChange(CameraPosition position) {

    }

    @Override
    public void onCameraChangeFinish(CameraPosition position) {
        addMarkersToMap();
    }

    @Override
    public void onInfoWindowClick(Marker marker) {

    }

    @Override
    public void onMapLoaded() {
        mAMap.moveCamera(CameraUpdateFactory.newLatLng(marker1));
        mAMap.moveCamera(CameraUpdateFactory.zoomTo(11.5f));

    }

    @Override
    public boolean onMarkerClick(Marker marker) {
        return false;
    }

    @Override
    public void onMarkerDragStart(Marker marker) {

    }

    @Override
    public void onMarkerDrag(Marker marker) {

    }

    @Override
    public void onMarkerDragEnd(Marker marker) {

    }

    @Override
    public void activate(OnLocationChangedListener listener) {
        mListener = listener;
        if (mlocationClient == null) {
            //初始化定位
            mlocationClient = new AMapLocationClient(this);
            //初始化定位参数
            mLocationOption = new AMapLocationClientOption();
            //设置定位回调监听
            mlocationClient.setLocationListener(this);
            //设置为高精度定位模式
            mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
            //设置定位参数
            mlocationClient.setLocationOption(mLocationOption);
            // 此方法为每隔固定时间会发起一次定位请求，为了减少电量消耗或网络流量消耗，
            // 注意设置合适的定位时间的间隔（最小间隔支持为2000ms），并且在合适时间调用stopLocation()方法来取消定位请求
            // 在定位结束后，在合适的生命周期调用onDestroy()方法
            // 在单次定位情况下，定位无论成功与否，都无需调用stopLocation()方法移除请求，定位sdk内部会移除
            mlocationClient.startLocation();//启动定位
        }
    }

    @Override
    public void deactivate() {
        mListener = null;
        if (mlocationClient != null) {
            mlocationClient.stopLocation();
            mlocationClient.onDestroy();
        }
        mlocationClient = null;
    }

    @Override
    public void onLocationChanged(AMapLocation amapLocation) {
        if (mListener != null && amapLocation != null) {
            if (amapLocation != null
                    && amapLocation.getErrorCode() == 0) {
                mListener.onLocationChanged(amapLocation);// 显示系统小蓝点
            } else {
                String errText = "定位失败," + amapLocation.getErrorCode() + ": " + amapLocation.getErrorInfo();
                Log.e("AmapErr", errText);
            }
        }
    }
}
